//
//  WebViewController.m
//  TrainMore
//
//  Created by yuan wei on 14-8-7.
//  Copyright (c) 2014年 yuan wei. All rights reserved.
//

#import "WebViewController.h"
#import "MyCenterViewController.h"

@interface WebViewController ()<UIWebViewDelegate,UIAlertViewDelegate>
{
    NSString  *titleName;
    NSString  *requestUrl;
    NSDictionary *dicInfo;

    NSString *_shareTitie;      //分享标题
    NSString *_shareDesc;       //分享简介
    NSString *_sharePicture;    //分享图片
    NSString *_shareUrl;        //分享url
}
@property(nonatomic,weak) UIWebView *webView;
@property(nonatomic,weak) UIActivityIndicatorView *activity;

@end

@implementation WebViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id)initWithTitle:(NSString *)_titleName withRequest:(NSString *)url
{
    self = [super init];
    if (self)
    {
        // Custom initialization
        
        titleName = _titleName;
        requestUrl = url;
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = titleName;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(0, 0, 20, 20);
    [button addTarget:self action:@selector(shareButton) forControlEvents:UIControlEventTouchUpInside];
    button.imageView.contentMode=UIViewContentModeScaleAspectFit;
    button.adjustsImageWhenHighlighted = NO;
    [button setBackgroundImage:[UIImage imageNamed:@"img_share"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"img_share"] forState:UIControlStateHighlighted];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    UIWebView *webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - 64)];
    self.webView = webView;
    _webView.userInteractionEnabled=YES;
    _webView.delegate = self;
    _webView.backgroundColor=[UIColor clearColor];
    [self.view addSubview:_webView];
    
    UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activity.center = _webView.center;
    [self.view addSubview:activity];
    self.activity = activity;
    
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:requestUrl] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60.0];
    [_webView loadRequest:request];
    
    if (self.enterWebViewBlock) {
        self.enterWebViewBlock();
    }
}

#pragma mark ------分享
-(void)shareButton
{
    //URL
//    NSString *urlStr = nil;
//    //分享内容
//    NSString *shareString = nil;
//    int shareType;

}

- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        return;
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark ------webview代理
-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [self.activity startAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.activity stopAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self.activity stopAnimating];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
//    NSString *urlStr=[[request URL] absoluteString];

//    NSURL *url=[NSURL URLWithString:urlStr];

    return YES;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
